package com.xd.common.util;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

/**
 * AES 是一种可逆加密算法，对用户的敏感信息加密处理 对原始数据进行AES加密后，在进行Base64编码转化；
 */
public class AESCBCUtil {
    /*
     * 加密用的Key 可以用26个字母和数字组成 此处使用AES-128-CBC加密模式，key需要为16位。
     */
    private String sKey = "94kl35k25d3t2rk2";
    private String ivParameter = "k394kf44lf1pyq8k";
    private static AESCBCUtil instance = null;

    private AESCBCUtil() {

    }

    public static AESCBCUtil getInstance() {
        if (instance == null)
            instance = new AESCBCUtil();
        return instance;
    }

    //
//    // 加密
//    public static String encrypt(String sSrc,String key,String iv0) {
//        Cipher cipher = null;
//        try {
//            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            byte[] raw = key.getBytes();
//            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//            IvParameterSpec iv = new IvParameterSpec(iv0.getBytes());// 使用CBC模式，需要一个向量iv，可增加加密算法的强度
//            cipher.init(Cipher.ENCRYPT_MODE, skeySpec, iv);
//            byte[] encrypted = cipher.doFinal(sSrc.getBytes("utf-8"));
//            return new BASE64Encoder().encode(encrypted);// 此处使用BASE64做转码。
//        } catch (Exception e) {
//            throw new RuntimeException(e);
//        }
//    }
    public static String encrypt(String plaintext, String key, String iv)  {
        SecretKeySpec secretKeySpec = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "AES");
        IvParameterSpec ivParameterSpec = new IvParameterSpec(iv.getBytes(StandardCharsets.UTF_8));

        Cipher cipher = null;
        try {
            cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);
            byte[] encryptedData = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(encryptedData);
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public static void main(String[] args) {
        System.out.println(AESUtil.encryptCBC("12", "febd1c57100212e4", "ebfe32f481246d30"));
        System.out.println(encrypt("12", "febd1c57100212e4", "ebfe32f481246d30"));
    }
}

//    // 解密
//    public static String decrypt(String sSrc) throws Exception {
//        try {
//            byte[] raw = sKey.getBytes("ASCII");
//            SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES");
//            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
//            IvParameterSpec iv = new IvParameterSpec(ivParameter.getBytes());
//            cipher.init(Cipher.DECRYPT_MODE, skeySpec, iv);
//            byte[] encrypted1 = new BASE64Decoder().decodeBuffer(sSrc);// 先用base64解密
//            byte[] original = cipher.doFinal(encrypted1);
//            String originalString = new String(original, "utf-8");
//            return originalString;
//        } catch (Exception ex) {
//            return null;
//        }
//    }
//
//    public static void main(String[] args) throws Exception {
//        // 需要加密的字串
//        String cSrc = "我来自www.wenhq.com";
//        System.out.println(cSrc);
//        // 加密
//        long lStart = System.currentTimeMillis();
//        String enString = AESCBCUtil.getInstance().encrypt(cSrc);
//        System.out.println("加密后的字串是：" + enString);
//
//        long lUseTime = System.currentTimeMillis() - lStart;
//        System.out.println("加密耗时：" + lUseTime + "毫秒");
//        // 解密
//        lStart = System.currentTimeMillis();
//        String DeString = AESCBCUtil.getInstance().decrypt(enString);
//        System.out.println("解密后的字串是：" + DeString);
//        lUseTime = System.currentTimeMillis() - lStart;
//        System.out.println("解密耗时：" + lUseTime + "毫秒");
//    }
